package com.theatre.userservice.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.theatre.userservice.annotation.RequirePermission;
import com.theatre.userservice.exception.BusinessException;
import com.theatre.userservice.model.dto.AuditDTO;
import com.theatre.userservice.model.entity.Publisher;
import com.theatre.userservice.model.entity.Script;
import com.theatre.userservice.model.entity.Shop;
import com.theatre.userservice.model.vo.Result;
import com.theatre.userservice.service.PublisherService;
import com.theatre.userservice.service.ScriptService;
import com.theatre.userservice.service.ShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/audit")
@Tag(name = "资质审核接口", description = "提供资质审核相关的接口")
public class AuditController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private PublisherService publisherService;

    @Autowired
    private ScriptService scriptService;

    /**
     * 更新商户状态
     */
    @RequirePermission("content:manage")
    @PostMapping("/shop/status")
    @Operation(summary = "更新商户状态", description = "更新商户状态")
    public Result<String> updateShopStatus(@RequestBody AuditDTO auditDTO) {
        Shop shop = shopService.getById(auditDTO.getId());
        if (shop == null) {
            throw new BusinessException("商户不存在");
        }
        shop.setStatus(auditDTO.getStatus());
        shop.setRemark(auditDTO.getRemark());
        shopService.updateById(shop);
        return Result.success("更新商户状态成功");
    }

    // ---- 状态为0的商户列表
    @RequirePermission("content:manage")
    @GetMapping("/shop/list/zero")
    @Operation(summary = "所有待审核商户列表", description = "仅返回状态为0（待审核）的商户信息")
    public Result<List<Shop>> getPendingShopList() {
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getStatus, 0);
        List<Shop> shopList = shopService.list(wrapper);
        return Result.success(shopList);
    }

    // ---- 所有商户列表（不论状态）
    @RequirePermission("content:manage")
    @GetMapping("/shop/list")
    @Operation(summary = "所有商户列表", description = "返回所有商户信息，不论状态")
    public Result<List<Shop>> getAllShopList() {
        List<Shop> shopList = shopService.list();
        return Result.success(shopList);
    }

    /**
     * 查询单个商户资质信息
     */
    @RequirePermission("content:manage")
    @GetMapping("/shop/get/{id}")
    @Operation(summary = "指定商户资质信息", description = "根据ID查询单个商户的详细资质信息")
    public Result<Shop> getShop(@PathVariable Long id) {
        Shop shop = shopService.getById(id);
        if (shop == null) {
            throw new BusinessException("商户不存在");
        }
        return Result.success(shop);
    }

    // ==================== 发行商 ====================

    @RequirePermission("content:manage")
    @PostMapping("/publisher/status")
    @Operation(summary = "更新发行商状态", description = "审核更新发行商资质状态（0:待审核,1:通过,2:不通过）")
    public Result<String> updatePublisherStatus(@RequestBody AuditDTO publisherAuditDTO) {
        Publisher publisher = publisherService.getById(publisherAuditDTO.getId());
        if (publisher == null) {
            throw new BusinessException("发行商不存在");
        }
        publisher.setStatus(publisherAuditDTO.getStatus());
        publisher.setRemark(publisherAuditDTO.getRemark());
        publisherService.updateById(publisher);
        return Result.success("更新发行商状态成功");
    }

    // ---- 状态为0的发行商列表
    @RequirePermission("content:manage")
    @GetMapping("/publisher/list/pending")
    @Operation(summary = "所有待审核发行商列表", description = "仅返回状态为0（待审核）的发行商信息")
    public Result<List<Publisher>> getPendingPublisherList() {
        LambdaQueryWrapper<Publisher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Publisher::getStatus, 0);
        List<Publisher> publisherList = publisherService.list(wrapper);
        return Result.success(publisherList);
    }

    // ---- 所有发行商列表
    @RequirePermission("content:manage")
    @GetMapping("/publisher/list")
    @Operation(summary = "所有发行商列表", description = "返回所有发行商信息，不论状态")
    public Result<List<Publisher>> getAllPublisherList() {
        List<Publisher> publisherList = publisherService.list();
        return Result.success(publisherList);
    }

    // ---- 单个发行商信息
    @RequirePermission("content:manage")
    @GetMapping("/publisher/get/{id}")
    @Operation(summary = "单个发行商信息", description = "根据ID回显发行商资质信息")
    public Result<Publisher> getPublisher(@PathVariable Long id) {
        Publisher publisher = publisherService.getById(id);
        if (publisher == null) {
            throw new BusinessException("发行商不存在");
        }
        return Result.success(publisher);
    }

    // ==================== 剧本 ====================

    @RequirePermission("content:manage")
    @PostMapping("/script/status")
    @Operation(summary = "更新剧本状态", description = "更新剧本状态（0:待审核,1:通过,2:不通过）")
    public Result<String> updateScriptStatus(@RequestBody AuditDTO scriptAuditDTO) {
        Script script = scriptService.getById(scriptAuditDTO.getId());
        if (script == null) {
            throw new BusinessException("剧本不存在");
        }
        script.setStatus(scriptAuditDTO.getStatus());
        scriptService.updateById(script);
        return Result.success("更新剧本状态成功");
    }

    // ---- 状态为0的剧本列表
    @RequirePermission("content:manage")
    @GetMapping("/script/list/pending")
    @Operation(summary = "所有待审核剧本列表", description = "仅返回状态为0（待审核）的剧本信息")
    public Result<List<Script>> getPendingScriptList() {
        LambdaQueryWrapper<Script> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Script::getStatus, 0);
        List<Script> scriptList = scriptService.list(wrapper);
        return Result.success(scriptList);
    }

    // ---- 所有剧本列表
    @RequirePermission("content:manage")
    @GetMapping("/script/list")
    @Operation(summary = "所有剧本列表", description = "返回所有剧本信息，不论状态")
    public Result<List<Script>> getAllScriptList() {
        List<Script> scriptList = scriptService.list();
        return Result.success(scriptList);
    }

    // ---- 单个剧本信息
    @RequirePermission("content:manage")
    @GetMapping("/script/get/{id}")
    @Operation(summary = "单个剧本信息", description = "根据ID回显剧本资质信息")
    public Result<Script> getScript(@PathVariable Long id) {
        Script script = scriptService.getById(id);
        if (script == null) {
            throw new BusinessException("剧本不存在");
        }
        return Result.success(script);
    }
}
